package com.hua.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hua.entities.Dish;
import com.hua.entities.DishDto;
import com.hua.entities.DishFlavor;
import com.hua.mapper.DishMapper;
import com.hua.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorServiceImpl dishFlavorService;


    public boolean saveAndFlavors(DishDto dishDto){
        this.save(dishDto);
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map(flavor ->{
                flavor.setDishId(id);
                return flavor;
        }).collect(Collectors.toList());

        boolean b = dishFlavorService.saveBatch(flavors);
        return b;
    }

}
